package com.ruoyi.nucleicacid.service.impl;

import java.util.List;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.nucleicacid.domain.Household;
import com.ruoyi.nucleicacid.service.IHouseholdService;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.nucleicacid.mapper.userRegisterMapper;
import com.ruoyi.nucleicacid.domain.UserRegister;
import com.ruoyi.nucleicacid.service.IuserRegisterService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 核酸注册Service业务层处理
 *
 * @author ruoyi
 * @date 2022-10-06
 */
@Service
public class userRegisterServiceImpl implements IuserRegisterService {
    @Autowired
    private userRegisterMapper userRegisterMapper;
    @Autowired
    private IHouseholdService householdService;
    @Autowired
    private RemoteUserService remoteUserService;

    /**
     * 查询核酸注册
     *
     * @param id 核酸注册主键
     * @return 核酸注册
     */
    @Override
    public UserRegister selectuserRegisterById(Long id) {
        return userRegisterMapper.selectuserRegisterById(id);
    }

    /**
     * 查询核酸注册列表
     *
     * @param userRegister 核酸注册
     * @return 核酸注册
     */
    @Override
    public List<UserRegister> selectuserRegisterList(UserRegister userRegister) {
        return userRegisterMapper.selectuserRegisterList(userRegister);
    }

    /**
     * 新增核酸注册
     *
     * @param userRegister 核酸注册
     * @return 结果
     */
    @Override
    @Transactional
    public int insertuserRegister(UserRegister userRegister) {
        Household household = new Household();
        user2HouseHlod(userRegister, household);
        householdService.insertHousehold(household);
        userRegister.setHoldid(household.getId());
        SysUser sysUser = getSysUser(userRegister);
        remoteUserService.registerUserInfo(sysUser, SecurityConstants.FROM_SOURCE);
        return userRegisterMapper.insertuserRegister(userRegister);

    }

    private SysUser getSysUser(UserRegister userRegister) {
        SysUser sysUser = new SysUser();
        sysUser.setUserName(userRegister.getName());
        sysUser.setNickName(userRegister.getName());
        sysUser.setPhonenumber(userRegister.getNumber());
        sysUser.setSex(userRegister.getSex());
        sysUser.setPassword("123456");
        return sysUser;
    }


    private void user2HouseHlod(UserRegister userRegister, Household household) {
        household.setFloorid(userRegister.getFloorid());
        household.setName(userRegister.getName());
        household.setNumber(userRegister.getNumber());
        household.setHaddress(userRegister.getHousehold());
        household.setStatus(1L);
    }

    /**
     * 修改核酸注册
     *
     * @param userRegister 核酸注册
     * @return 结果
     */
    @Override
    public int updateuserRegister(UserRegister userRegister) {
        return userRegisterMapper.updateuserRegister(userRegister);

    }

    /**
     * 批量删除核酸注册
     *
     * @param ids 需要删除的核酸注册主键
     * @return 结果
     */
    @Override
    public int deleteuserRegisterByIds(Long[] ids) {
        return userRegisterMapper.deleteuserRegisterByIds(ids);
    }

    /**
     * 删除核酸注册信息
     *
     * @param id 核酸注册主键
     * @return 结果
     */
    @Override
    public int deleteuserRegisterById(Long id) {
        return userRegisterMapper.deleteuserRegisterById(id);
    }
}
